package com.zjh.mf.mfcorelib.util;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2016/4/19.
 */
public class DataUtil {
    public static String getTime(Date date, String formatString){
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        return format.format(date);
    }

    public static String getTime(long time){
        if(time == 0) return "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(time);
    }

    public static String getTime(long time, String formatString){
        if(time == 0) return "";
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        return format.format(time);
    }

    public static String getTime(int time){
        if(time == 0) return "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(Long.parseLong(time + "" + "000"));
    }

    public static String getTime(int time, String formatString){
        if(time == 0) return "";
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        return format.format(Long.parseLong(time + "" + "000"));
    }

    /**
     * 将json时间字符串转换为Date
     *
     * @param input
     * @return
     */
    public static String JsonTimeToDate(String input, String formatString) {
        SimpleDateFormat df = new SimpleDateFormat(formatString, Locale.getDefault());
        if (input.contains("T"))
            input = input.replace('T', ' ');
        if (input.contains("Z"))
            input = input.replace("Z", "+0000");
        try {
            Date date = df.parse(input);
            return getTime(date,formatString);
        } catch (ParseException e) {
            Log.e("parse error", "JsonTimeToDate转换时间错误：" + input);
            return null;
        }
    }

    /**
     * 转换时间到long
     *
     * @param input
     * @return
     */
    public static long getTimeLong(String input, String formatString) {
        SimpleDateFormat df = new SimpleDateFormat(formatString, Locale.getDefault());
        try {
            Date date = df.parse(input);
            return date.getTime();
        } catch (ParseException e) {
            Log.e("parse error", "转换时间错误：" + input);
            return 0;
        }
    }


}
